package com.gmzg.gmzgnews.topic.view;



import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.gmzg.gmzgnews.R;
import com.gmzg.gmzgnews.topic.adapters.TopicFollowAdapter;
import com.gmzg.gmzgnews.topic.impl.TopicFollowService;
import com.gmzg.gmzgnews.topic.model.Food;
import com.gmzg.gmzgnews.topic.model.FoodParent;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static android.content.ContentValues.TAG;


public class TopicFollowFragment extends BaseFragment implements Callback<FoodParent> {

    private RecyclerView mRecyclerViewFllow;
    private TopicFollowService mService;
    private TopicFollowAdapter mAdapter;
    //use in init mAdapter
    private ArrayList<Food> mNewsList;
    private Call<FoodParent> mCall;

    public TopicFollowFragment() {
        // Required empty public constructor
    }

    //Set fragment's title
    @Override
    public String getFragmentTitle() {
        return "关注";
    }


    ///////////////////////////////////////////////////////////////////////////
    // lifeCycle Method
    ///////////////////////////////////////////////////////////////////////////

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_topic_follow, container, false);

        initView(view);
        retrofitFun();
        recyclerViewFun();
        return view;
    }

    @Override
    public void onPause() {
        if (mCall != null) {
            mCall.cancel();
        }
        Log.d(TAG, "onPause: Follow");
        super.onPause();
    }


    public void initView(View view) {
        mRecyclerViewFllow = ((RecyclerView) view.findViewById(R.id.topic_follow_recycler_view));
    }

    // Load recyclerView
    public void recyclerViewFun(){
        RecyclerView.LayoutManager layoutManager =
                new LinearLayoutManager(
                        getContext(),
                        LinearLayout.VERTICAL,
                        false
                );
        mRecyclerViewFllow.setLayoutManager(layoutManager);
        mNewsList = new ArrayList<>();
        mAdapter = new TopicFollowAdapter(getContext(), mNewsList);
        mRecyclerViewFllow.setAdapter(mAdapter);

    }


    ///////////////////////////////////////////////////////////////////////////
    // Retrofit Part
    ///////////////////////////////////////////////////////////////////////////
    private void retrofitFun() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://www.tngou.net")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        mService = retrofit.create(TopicFollowService.class);
        mCall = mService.getList("cook", 0, 1, 20);
        mCall.enqueue(this);
    }


    @Override
    public void onResponse(Call<FoodParent> call, Response<FoodParent> response) {
        List<Food> foodList = response.body().getFoodList();
        mAdapter.addAll(foodList);
    }

    @Override
    public void onFailure(Call<FoodParent> call, Throwable t) {
        if (!mCall.isCanceled()) {
            Toast.makeText(getContext(), "网络出错", Toast.LENGTH_SHORT).show();
        }
    }


}
